package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.dao.AuthorityDao;
import com.itheima.pojo.Permission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

@Service(interfaceClass = AuthorityService.class)
@Transactional
public class AuthorityServiceImpl implements AuthorityService {
    @Autowired
    private AuthorityDao authorityDao;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Permission> page = authorityDao.findAll(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public Result add(Permission permission) {
       try {
           authorityDao.add(permission);
       }catch (Exception e){

       }
        return new Result(true,"添加权限成功");
    }

    @Override
<<<<<<< HEAD
    public void delById(Integer id) {
        authorityDao.delById(id);
=======
    public Result delById(Integer id) {
        //先查询关联表,是否被关联了
       Integer ido = authorityDao.findRoleByPermissionId(id);
       if(ido!=null && ido>0){
           //已经被关联了;
           return new Result(false,"该地址已经被关联了,不能被删除");
       }else {
           authorityDao.delById(id);
           return new Result(true,"删除成功");
       }

>>>>>>> cebc61dc7c2086549f6b7e3e5922bd595f26ae10
    }

    @Override
    public Permission findById(Integer id) {
      return  authorityDao.findById(id);
    }

    @Override
    public void update(Permission permission) {
        authorityDao.update(permission);
    }
}
